use std::io::{self, Write};

mod lexer;
use lexer::Lexer;

mod parser;
use parser::Parser;

mod evaluator;
use evaluator::ASTEvaluator;

fn main() {
  // let mut lexer = Lexer::new("1");
  // let tokens = lexer.tokenizes();
  // for token in tokens {
  //   println!("{:?}", token);
  // }

  // let mut lexer = Lexer::new("2+3");
  // let tokens = lexer.tokenizes();
  // for token in &tokens {
  //   println!("{token:?}");
  // }
  // println!("---------------------分割线------------------------");

  // let mut parser = Parser::new(tokens);
  // parser.print_ast();

  let mut input = String::new();
  let mut stdout = io::stdout();
  let stdin = io::stdin();
  loop {
    write!(stdout, "> ").expect("Should have written prompt string >");

    stdout.flush().unwrap();
    stdin.read_line(&mut input).expect("fail to read stdin");

    if input.len() == 0 {
      break;
    }

    let mut lexer = Lexer::new(input.as_str());
    let tokens = lexer.tokenizes();
    for token in &tokens {
      writeln!(stdout, "{token:?}").expect("Token should have been written");
    }

    println!("---------------------分割线------------------------");

    let mut parser = Parser::new(tokens);
    let ast = parser.print_ast();

    println!("---------------------分割线------------------------");

    let eval = ASTEvaluator::new();
    let result = eval.print_result(ast);
    println!("Result: {:?}", result);

    input.clear();
  }
}
